<?php

// 设置当前脚本编码
header('Content-type:text/html;charset=utf-8');

// 不限制脚本执行时间
set_time_limit(0);

// 打开链接
$Content = file_get_contents('https://book.zongheng.com/showchapter/984807.html');

// 书名正则
$BookNameReg = '/<div\s*class="book-meta">.*<h1>(.*)<\/h1>.*<\/div>/imsU';

preg_match($BookNameReg, $Content, $BookNameResult);

$BookName = $BookNameResult[1] ?? '';

if (empty($BookName)) {
  echo '书名为空';
  exit;
}

// 获取该书籍章节
$ChaptersReg = '/<ul\s*class="chapter-list\s*clearfix">(.*)<\/ul>/imsU';

preg_match_all($ChaptersReg, $Content, $ChaptersResult);

// 获取某卷里的章节
$Chapters = $ChaptersResult[1][1] ?? [];

if (empty($Chapters)) {
  echo '章节列表为空';
  exit;
}

$reg = '/<a\s*href="(.*)".*>(.*)<\/a>/imsU';

preg_match_all($reg, $Chapters, $res);

$UrlList = $res[1] ?? [];
$TitleList = $res[2] ?? [];

if (empty($UrlList)) {
  echo '章节链接列表为空';
  exit;
}

if (empty($TitleList)) {
  echo '标题列表为空';
  exit;
}

if (count($UrlList) != count($TitleList)) {
  echo '章节地址总数与章节标题总数不一致';
  exit;
}

$result = '';

foreach ($UrlList as $key => $url) {
  // 给一个默认 user_agent 让他认为自己是浏览器
  ini_set('user_agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)');

  // 打开章节
  $ChapterContent = file_get_contents($url);

  if (empty($ChapterContent)) {
    continue;
  }

  $timeReg = '/<div\s*class="bookinfo">.*<span>更新时间：<i>(.*)<\/i><\/span>.*<\/div>/imsU';

  preg_match($timeReg, $ChapterContent, $TimeResult);

  $time = $TimeResult[1] ?? '';

  if (empty($time)) {
    continue;
  }

  $ContentReg = '/<div\s*class="content".*>(.*)<\/div>/imsU';

  preg_match($ContentReg, $ChapterContent, $ContentResult);

  $content = $ContentResult[1] ?? '';

  if (empty($content)) {
    continue;
  }

  // 把p标签都换成换行符
  $content = preg_replace('/<p>|<\/p>/', "\r\n", $content);

  // 去掉html或者php标签
  $content = strip_tags($content);

  $result .= $TitleList[$key];

  $result .= "\r\n更新时间：$time";

  $result .= "\r\n" . $content . "\r\n";
}

$length = file_put_contents('./assets/book/' . $BookName . '.txt', $result);

if ($length > 0) {
  echo '采集了' . $length . '字';
  exit;
} else {
  echo "保存txt文件失败\n";
}
